<?php
/**
 * 用户通话记录检查
 * User: GRAY
 * Date: 16/8/11
 * Time: 下午10:23
 */

namespace Home\Common\Hook;


use Common\Library\BusinessException;
use Common\Library\Constant;
use Common\Vendor\Hook\BaseHook;
use Think\Log;

class CallRecord extends BaseHook
{

    public function execute($data, $params = [])
        {
            $customerId = $data['id'];
            if ($customerId) {
                //通话记录数<10
                $recordCount = D("CustomerCallRecord")->where(['customer_id'=>$customerId])->count();
                //Log::write("Customer contact:".$contactCount, Log::INFO);
                if ($recordCount < $params['min']) {
                    BusinessException::throwException(Constant::NO_ENOUGH_RECORD);
                }
                //外呼时间<=30分钟
                $Result =D("CustomerCallRecord")->query("SELECT SUM(`duration_time`) as call_out_time  FROM  `customer_call_record` WHERE  `customer_id` ='$customerId' and `in_out`=2");
                $call_out_time = $Result[0]['call_out_time'];
                if($call_out_time<=$params['call_out_time']) {
                    BusinessException::throwException(Constant::NO_ENOUGH_RECORD1);
                }
                //最早通话时间>7天
                $Result =D("CustomerCallRecord")->query("SELECT `call_time` FROM  `customer_call_record` WHERE  `customer_id` ='$customerId' order by `call_time` asc limit 1");
                $last_call_time = substr($Result[0]['call_time'],0,10);
                $diff = time() - $last_call_time;
                if($diff < $params['begin_call_days'] * 24 *3600) {
                    BusinessException::throwException(Constant::NO_ENOUGH_RECORD2);
                }
            }
            return $data;
        }
} 
